layui.define(
  [
    "laydate",
    "table",
    "laypage",
    "form",
    "element",
    "layer",
    "transfer",
    "util",
    "tree",
  ],
  function (exports) {
    var laydate = layui.laydate,
      laypage = layui.laypage,
      layer = layui.layer,
      form = layui.form,
      loadIndex = "",
      table = layui.table,
      util = layui.util,
      tree = layui.tree;
    //左边存储数组
    leftArr = [];
    leftArrNum = [];
    leftItem = "";
    fileName = "";

    //右边存储数组
    rightArr = [];
    rightArrNum = [];
    rightItem = "";

    rightId = [];

    let treeData = [];
    let treeData2 = [];
    let proIDX = "";

    let dataId = [];
    let parentId = "";

    //  列表和表格切换的操作
    if (layui.sessionData("isSwitch").key) {
      $(".switchContentBtn i")
        .removeClass("icon-caidan")
        .addClass("icon-caidan1");
      $(".projectManagementTable").show();
      $(".projectManagementList").hide();
    } else {
      $(".switchContentBtn i")
        .removeClass("icon-caidan1")
        .addClass("icon-caidan");
      $(".projectManagementTable").hide();
      $(".projectManagementList").show();
    }
    $(".switchContentBtn").click(function () {
      if (!layui.sessionData("isSwitch").key) {
        $(".switchContentBtn i")
          .removeClass("icon-caidan")
          .addClass("icon-caidan1");
        $(".projectManagementTable").show();
        $(".projectManagementList").hide();
        layui.sessionData("isSwitch", {
          key: "key",
          value: true,
        });
      } else {
        $(".switchContentBtn i")
          .removeClass("icon-caidan1")
          .addClass("icon-caidan");
        $(".projectManagementTable").hide();
        $(".projectManagementList").show();
        layui.sessionData("isSwitch", {
          key: "key",
          value: false,
        });
      }
    });

    //日期范围
    laydate.render({
      elem: "#monthRange",
      type: "month",
      range: ["#monthRangeStart", "#monthRangeEnd"],
    });

    //搜索
    var flag = true;
    form.on("submit(searchFolderBtn)", function (data) {
      if (flag) {
        var data = data.field;
        table.reload("projectManagementTable", {
          where: {
            sort: "1",
            startTime: data.startTime,
            endTime: data.endTime,
            projectName: data.projectName,
          },
        });
        renderProduct1(
          page,
          limit,
          data.startTime,
          data.endTime,
          data.projectName
        );
        flag = false;
        $(this)
          .children()
          .removeClass("layui-icon-search")
          .addClass("layui-icon-close");
      } else {
        $("input").val("");
        table.reload("projectManagementTable", {
          where: {
            sort: "1",
            startTime: "",
            endTime: "",
            projectName: "",
          },
        });
        renderProduct1();
        flag = true;
        $(this)
          .children()
          .removeClass("layui-icon-close")
          .addClass("layui-icon-search");
      }
    });

    // 创建文件夹
    $(".createFolderBtn").on("click", function () {
      layer.open({
        title: '<i class="fa fa-plus"></i>&nbsp;新增文件夹',
        type: 1,
        area: ["30%", "28%"],
        shadeClose: true,
        // btn: ["确定", "取消"],
        // btnAlign: "c",
        content: $("#createFolderForm").html(),
        success: function (layero, index) {
          form.on("submit(createFolderSubmit)", function (d) {
            $.ajax({
              type: "POST",
              url: base_url + "/projectManage/createFolder",
              contentType: "application/json; charset=utf-8",
              async: true,
              data: JSON.stringify(d.field),
              dataType: "json",
              headers: {
                Accept: "application/json; charset=utf-8",
                Authorization: layui.sessionData("userToken").key,
              },
              success: function (res) {
                console.log(res);
              },
            });
          });
        },
      });
    });
    // 列表接口
    table.render({
      elem: "#projectManagementTable",
      skin: "nob",
      even: true,
      url: base_url + "/projectManage/queryProListByProperty",
      headers: {
        Accept: "application/json; charset=utf-8",
        Authorization: layui.sessionData("userToken").key,
      },
      method: "post",
      contentType: "application/json",
      cellMinWidth: 80,
      height: "full-120",
      parseData: function (res) {
        return {
          code: 0,
          count: res.data.projectList.total,
          data: res.data.projectList.list,
          msg: res.data.message || "请求成功",
        };
      },
      cols: [
        [
          {
            event: "projectFilesNext",
            templet:
              '<div class="filesIcon"><img  src="../../layuiadmin/images/filesIcon.png"></div>',
          },
          {
            field: "proNmae",
            title: "项目名称",
            align: "center",
            templet: function (item) {
              return (
                "<div title=" +
                item.proNmae +
                " class='whiteSpace'>" +
                item.proNmae +
                "</div>"
              );
            },
          },
          { field: "userName", title: "创建者", align: "center" },
          { field: "date", title: "创建时间", align: "center" },
          { field: "date", title: "更新时间", align: "center" },
        ],
      ],
      page: true,
      limit: 5,
      limits: [5, 10, 15, 20, 25, 30],
      where: { sort: "1" },
      text: { none: "无数据" },
      done: function (res, curr, count) {
        if (!layui.sessionData("curr").curr) {
          layui.sessionData("curr", {
            key: "curr",
            value: curr,
          });
        }

        // 表格渲染完成之后的回调
        var that = this.elem.next();
        res.data.forEach(function (item, index) {
          if (index % 2 == 0) {
            var tr = that
              .find(".layui-table-box tbody tr[data-index='" + index + "']")
              .css("background-color", "transparent");
          } else {
            var tr = that
              .find(".layui-table-box tbody tr[data-index='" + index + "']")
              .css("background", "#EDF2FB");
          }
        });
      },
    });
    //监听行单击事件（双击事件为：rowDouble）
    table.on("row(projectManagementTable)", function (obj) {
      var data = obj.data;
      $(this).attr(
        "lay-href",
        "./pages/projectManagementNextT.html?proID=" +
          data.proID +
          "&proType=" +
          data.proType
      );
    });
    var page = 1; //当前页
    var limit = 40; //每页显示的数目
    var resCount, resData;
    renderProduct1();

    //渲染展示文件的html页面
    function renderProductHtml(data) {
      layer.close(loadIndex);
      var str = ""; //用来存储html内容
      if (data.length > 0) {
        $.each(data, function (v, o) {
          str += `<li class="nextLevelFile moveOtherFile" data-proID="${
            o.proID
          }" data-proType="${o.proType}"><a title="${
            o.proNmae
          }"><img src="../../layuiadmin/images/filesIcon.png"/><p class="changename">${
            o.proNmae ? o.proNmae : ""
          }</p></a></li>`;
        });

        $(".projectManagementList #projectManagementListCon").html(str);
      } else {
        $(".projectManagementList #projectManagementListCon").html(
          "无相关项目"
        );
      }

      $(".moveOtherFile").on("click", function () {
        debugger;
        var titleText = $(this).children("a").children("p").text();
        if (!layui.sessionData("isMove").key) {
          debugger;
          var proID = $(this).attr("data-proID"),
            proType = $(this).attr("data-proType");
          location.href =
            "projectManagementNextT.html?proID=" +
            proID +
            "&proType=" +
            proType;
        } else {
          var proID = $(this).attr("data-proID");
          var zNodesData = "",
            zNodesRData = "";
          layer.open({
            title: titleText,
            type: 1,
            fix: false,
            resize: false,
            skin: "nextLevelFileSty",
            // 加上边框
            area: ["800px", "700px"],
            // 宽高
            content: $("#changeFile").html(),
            success: function (layero, index) {
              var moveDataList = new Array();
              //加载左侧按钮
              getLeftTreeData(proID, fileName);
              function getLeftTreeData(proID, fileName) {
                $.ajax({
                  async: false,
                  url: base_url + "/projectManage/selMoveFile",
                  headers: {
                    Accept: "application/json; charset=utf-8",
                    Authorization: layui.sessionData("userToken").key,
                  },
                  method: "post",
                  contentType: "application/json",
                  data: JSON.stringify({
                    proID: proID,
                    fileName: fileName,
                  }),
                  dataType: "json",
                  success: function (result) {
                    if (result.code == "200") {
                      treeData = result.data.zNodes;

                      zNodesData = getTreeData(treeData);
                      //left 左边tree展示-----------------------
                      tree.render({
                        elem: "#test12",
                        data: zNodesData,
                        showCheckbox: true, //是否显示复选框
                        id: "demoId1",
                        isJump: false, //是否允许点击节点时弹出新窗口跳转
                      });
                    }
                  },
                });
              }

              function getTreeData(treeData) {
                let arr = [];
                treeData.filter((msg) => {
                  if (msg.pId == -1) {
                    arr.push({
                      id: msg.id,
                      field: msg.pId,
                      title: msg.name,
                      checked: true,
                      spread: true,
                      children: getChildTree(treeData, msg.id),
                    });
                  }
                });
                return arr;
              }

              function getChildTree(treeData, id) {
                let obj = [];
                treeData.filter((res) => {
                  if (res.pId == id) {
                    obj.push({
                      id: res.id,
                      field: res.pId,
                      title: res.name,
                      checked: false,
                      spread: true,
                      children: getChildTree(treeData, res.id),
                    });
                  }
                });
                return obj;
              }

              $("#left_layui").click((data) => {
                let fileName = $(".leftInput").val();
                console.log(
                  "fileName-------------------------------------------------------",
                  fileName
                );
                getLeftTreeData(proID, fileName);
              });

              //按钮事件
              util.event("lay-demo", {
                getChecked: function (othis) {
                  var arrData = [];
                  dataId = [];
                  parentId = "";
                  var checkedData = tree.getChecked("demoId1"); //获取选中节点的数据

                  var selectData = checkedData[0].children[0].children;
                  if (checkedData.length == 0) {
                    layer.msg("请选择移动的文件", { icon: 5 });
                  } else {
                    childrenX(selectData);
                    function childrenX(selectData) {
                      selectData.forEach((obj) => {
                        arrData.push(obj);
                        dataId.push(obj.id);
                      });
                    }
                  }

                  checkedData2 = tree.getChecked("demoId2"); //获取选中节点的数据

                  if (checkedData2.length == 0) {
                    layer.msg("请选择移动的项目", { icon: 5 });
                  } else {
                    parentId = checkedData2[0].children[0].id;
                    arrData.filter((res) => {
                      return (res.pld = parentId);
                    });

                    zNodesRData.forEach((data) => {
                      if (data.id == checkedData2[0].id) {
                        arrData.filter((msg) => {
                          data.children[0].children.unshift(msg);
                        });
                      }
                    });

                    let leftObjTree = checkedData[0];
                    zNodesData = leftCheckObjTree();

                    function leftCheckObjTree() {
                      let arrData = "";
                      arrData = zNodesData.filter((item) => {
                        if (item.id == leftObjTree.id) {
                          return (item.children = leftObjTreeData(
                            item.children,
                            leftObjTree.children[0].id
                          ));
                        }
                      });
                      return arrData;
                    }

                    function leftObjTreeData(item, id) {
                      return item.filter((res) => {
                        if (res.id == id && res.children.length > 0) {
                          return (res.children = leftChildFun(
                            res.children,
                            leftObjTree.children[0].children
                          ));
                        } else {
                          return [];
                        }
                      });
                    }
                    function leftChildFun(array, data) {
                      var result = [];
                      var arrItem = [];

                      if (array.length == data.length) {
                        return (result = []);
                      } else {
                        array.forEach((arr, index) => {
                          arrItem.push(arr.id);
                        });

                        data.forEach((item, index) => {
                          if (arrItem.indexOf(item.id) != -1) {
                            arrItem.splice(arrItem.indexOf(item.id), 1);
                          } else {
                            arrItem.push(item.id);
                          }
                        });

                        array.filter((msg) => {
                          arrItem.filter((id) => {
                            if (msg.id == id) {
                              result.push(msg);
                            }
                          });
                        });

                        return result;
                      }
                    }
                    tree.render({
                      elem: "#test12",
                      data: zNodesData,
                      showCheckbox: true, //是否显示复选框
                      id: "demoId1",
                    });

                    tree.render({
                      elem: "#test13",
                      data: zNodesRData,
                      showCheckbox: true, //是否显示复选框
                      id: "demoId2",
                    });
                  }
                },
              });

              //加载右侧按钮
              getRightTreeData(proID, fileName);
              function getRightTreeData(proID, fileName) {
                $.ajax({
                  async: false,
                  url: base_url + "/projectManage/selMovePro",
                  headers: {
                    Accept: "application/json; charset=utf-8",
                    Authorization: layui.sessionData("userToken").key,
                  },
                  method: "post",
                  contentType: "application/json",
                  data: JSON.stringify({
                    proID: proID,
                    proName: fileName,
                  }),
                  dataType: "json",
                  success: function (result) {
                    if (result.code == "200") {
                      treeData2 = result.data.zNodes;
                      zNodesRData = getTreeDataFun();
                      // console.log("zNodesRData----", zNodesRData);
                      rightTree = zNodesRData;
                      reloadTreeData(rightTree);
                    }
                  },
                });
              }

              // ---------------------------------
              var treeDataVal = "";
              function reloadTreeData(rightTree) {
                tree.render({
                  elem: "#test13",
                  data: rightTree,
                  showCheckbox: true, //是否显示复选框
                  id: "demoId2",
                  isJump: false, //是否允许点击节点时弹出新窗口跳转
                  oncheck: function (obj, index) {
                    var data = obj.data; //得到当前点击的节点数据
                    var state = obj.checked;
                    var name = data.database_name; //勾选单个节点
                    var childrenData = data.children; //勾选父节点
                    if (state) {
                      if (!childrenData) {
                        //防止多次添加图层
                        for (var i = 0; i < databaseNameArr.length; i++) {
                          if (databaseNameArr[i] === name) {
                            return false;
                          }
                        }
                        databaseNameArr.push(name);
                      } else {
                        for (var j = 0; j < childrenData.length; j++) {
                          var childrenName = childrenData[j].database_name;
                          //防止多次添加图层
                          for (var i = 0; i < databaseNameArr.length; i++) {
                            if (databaseNameArr[i] === childrenName) {
                              return false;
                            }
                          }
                          databaseNameArr.push(childrenName);
                        }
                      }
                    } else {
                      if (!childrenData) {
                        //删除符合要求的值，并返回为真值的所有元素
                        _.remove(databaseNameArr, function (n) {
                          return n === name;
                        });
                      } else {
                        for (var j = 0; j < childrenData.length; j++) {
                          var childrenName = childrenData[j].database_name;
                          //删除符合要求的值，并返回为真值的所有元素
                          _.remove(databaseNameArr, function (n) {
                            return n === childrenName;
                          });
                        }
                      }
                    }
                  },
                  click: function (obj) {
                    console.log(obj.data); //得到当前点击的节点数据
                    console.log(obj.state); //得到当前节点的展开状态：open、close、normal
                    console.log(obj.elem); //得到当前节点元素

                    var data = obj.data; //获取当前点击的节点数据
                    if (obj.state == "open") {
                      var info = true;
                      data = rightCheckInfo(info);
                    } else {
                      var info = false;
                      data = rightCheckInfo(info);
                    }

                    // let rightObjTree = obj.data;

                    function rightCheckInfo(info) {
                      data.checked = info;
                      data.spread = info;
                      data.children.forEach((item) => {
                        item.checked = info;
                        item.spread = info;
                      });
                      return data;
                    }

                    // function rightObjTreeInfo(item, info) {
                    //   return item.filter((res) => {
                    //     res.checked = info;
                    //     res.spread = info;
                    //   });
                    // }

                    console.log(data);

                    tree.reload("demoId2", data);
                  },
                  reload: function () {
                    //重载实例
                    tree.reload("demoId2", {});
                  },
                });
              }

              //right 树结构
              function getTreeDataFun() {
                let arr1 = [];
                treeData2.forEach((msg) => {
                  if (msg.pId == -1) {
                    arr1.push({
                      id: msg.id,
                      field: msg.pId,
                      title: msg.name,
                      checked: false,
                      spread: false,
                      children: childrenData(msg.id),
                    });
                  }
                });
                return arr1;
              }

              function childrenData(idx) {
                let arr2 = [];
                treeData2.forEach((res) => {
                  if (res.pId == idx) {
                    arr2.push({
                      id: res.id,
                      field: res.pId,
                      title: res.name,
                      checked: false,
                      spread: false,
                      disabled: false,
                      children: getChildNode(res.id),
                    });
                  }
                });
                return arr2;
              }

              function getChildNode(idx) {
                let arr3 = [];
                treeData2.forEach((res) => {
                  if (res.pId == idx) {
                    arr3.push({
                      id: res.id,
                      field: res.pId,
                      title: res.name,
                      checked: false,
                      spread: false,
                      disabled: true,
                      children: [],
                    });
                  }
                });
                return arr3;
              }

              function rightDataFun() {
                var checkedData2 = tree.getChecked("demoId2"); //获取选中节点的数据
                console.log("11", checkedData2);
                let rightObjTree = checkedData2[0];
                zNodesRData = rightCheckObjTree();
                function rightCheckObjTree() {
                  zNodesRData.forEach((item) => {
                    if (item.id == rightObjTree.id) {
                      return (item.children = rightObjTreeData(
                        item.children,
                        rightObjTree.children[0].id
                      ));
                    }
                  });
                  return zNodesRData;
                }

                function rightObjTreeData(item, id) {
                  return item.filter((res) => {
                    if (res.id == id && res.children.length > 0) {
                      return (res.children = rightChildFun(
                        res.children,
                        rightObjTree.children[0].children
                      ));
                    } else {
                      return [];
                    }
                  });
                }

                function rightChildFun(array, data) {
                  var result = [];
                  var arrItem = [];

                  if (array.length == data.length) {
                    return (result = []);
                  } else {
                    array.forEach((arr, index) => {
                      arrItem.push(arr.id);
                    });

                    data.forEach((item, index) => {
                      if (arrItem.indexOf(item.id) != -1) {
                        arrItem.splice(arrItem.indexOf(item.id), 1);
                      } else {
                        arrItem.push(item.id);
                      }
                    });

                    array.filter((msg) => {
                      arrItem.filter((id) => {
                        if (msg.id == id) {
                          result.push(msg);
                        }
                      });
                    });

                    return result;
                  }
                }
              }

              //按钮事件
              util.event("lay-demo2", {
                getChecked: function (othis) {
                  var arrData2 = [];
                  var checkedData2 = tree.getChecked("demoId2"); //获取选中节点的数据
                  if (checkedData2.length == 0) {
                    layer.alert("请选择移动的节点", { shade: 0 });
                  } else {
                    var selectData2 = checkedData2[0].children[0].children;

                    childrenY(selectData2);
                    function childrenY(selectData2) {
                      selectData2.forEach((obj) => {
                        arrData2.push(obj);
                        dataId.push(obj.id);
                      });
                    }
                  }

                  var checkedData1 = tree.getChecked("demoId1"); //获取选中节点的数据
                  if (checkedData1.length == 0) {
                    layer.alert("请选择移动的节点", { shade: 0 });
                  } else {
                    arrData2.forEach((data) => {
                      data.field = checkedData1[0].children[0].id;
                      zNodesData[0].children[0].children.unshift(data);
                    });
                    let rightObjTree = checkedData2[0];
                    zNodesRData = rightCheckObjTree();
                    function rightCheckObjTree() {
                      zNodesRData.forEach((item) => {
                        if (item.id == rightObjTree.id) {
                          return (item.children = rightObjTreeData(
                            item.children,
                            rightObjTree.children[0].id
                          ));
                        }
                      });
                      return zNodesRData;
                    }

                    function rightObjTreeData(item, id) {
                      return item.filter((res) => {
                        if (res.id == id && res.children.length > 0) {
                          return (res.children = rightChildFun(
                            res.children,
                            rightObjTree.children[0].children
                          ));
                        } else {
                          return [];
                        }
                      });
                    }

                    function rightChildFun(array, data) {
                      var result = [];
                      var arrItem = [];

                      if (array.length == data.length) {
                        return (result = []);
                      } else {
                        array.forEach((arr, index) => {
                          arrItem.push(arr.id);
                        });

                        data.forEach((item, index) => {
                          if (arrItem.indexOf(item.id) != -1) {
                            arrItem.splice(arrItem.indexOf(item.id), 1);
                          } else {
                            arrItem.push(item.id);
                          }
                        });

                        array.filter((msg) => {
                          arrItem.filter((id) => {
                            if (msg.id == id) {
                              result.push(msg);
                            }
                          });
                        });

                        return result;
                      }
                    }

                    tree.render({
                      elem: "#test12",
                      data: zNodesData,
                      showCheckbox: true, //是否显示复选框
                      id: "demoId1",
                    });

                    tree.render({
                      elem: "#test13",
                      data: zNodesRData,
                      showCheckbox: true, //是否显示复选框
                      id: "demoId2",
                    });
                  }
                },
              });
              // 右侧搜索
              $("#right_layui").click((data) => {
                let fileName = $(".rightInput").val();
                getRightTreeData(proID, fileName);
              });
              // 确认移动
              $("#sureTransfer").click(() => {
                console.log("parentId", checkedData2);
                console.log("dataId", dataId);
                $.ajax({
                  async: false,
                  url: base_url + "/projectManage/moveFolder",
                  headers: {
                    Accept: "application/json; charset=utf-8",
                    Authorization: layui.sessionData("userToken").key,
                  },
                  method: "post",
                  contentType: "application/json",
                  data: JSON.stringify({
                    proID: checkedData2[0].id,
                    moveFileID: dataId,
                  }),
                  dataType: "json",
                  success: function (result) {
                    if (result.code == "200") {
                      layer.msg(result.message, { icon: 6 });
                      layer.close(index);
                    }
                  },
                });
              });
              $(".cancelBtn").click(function () {
                layer.close(index);
              });
              $.ajax({
                async: false,
                url: base_url + "/projectManage/selMoveFile",
                headers: {
                  Accept: "application/json; charset=utf-8",
                  Authorization: layui.sessionData("userToken").key,
                },
                method: "post",
                contentType: "application/json",
                data: JSON.stringify({
                  proID: proID,
                }),
                dataType: "json",
                success: function (result) {
                  if (result.code == "200") {
                    var moveData = result.data,
                      itemFileTitle = "",
                      itemFile = "";
                    var moveDataList = moveData["其他"];
                    if (moveDataList && moveDataList.length > 0) {
                      for (let item in moveDataList) {
                        itemFile +=
                          ' <dd data-id="' +
                          moveDataList[item].id +
                          '"><a href="javascript:;">' +
                          fileSuffixFun(moveDataList[item].fileName) +
                          moveDataList[item].fileName +
                          "</a></dd>";
                      }
                      itemFileTitle =
                        '<li class="layui-nav-item layui-nav-itemed"><a href="javascript:;"><img src="../../layuiadmin/images/filesIcon.png"/>其他</a><dl class="layui-nav-child">' +
                        itemFile +
                        "</dl></li>";
                      $(".projectFilesList .fileChangeBtn").show();
                    } else {
                      $(".projectFilesList .fileChangeBtn").hide();
                      itemFileTitle =
                        '<li class="layui-nav-item layui-nav-itemed"><a href="javascript:;"><img src="../../layuiadmin/images/filesIcon.png"/>其他</a><dl class="layui-nav-child"><dd style="font-size:14px;color:#999999;text-align:center;">空文件夹</dd></dl></li>';
                    }
                    function fileSuffixFun(item) {
                      if (item.indexOf(".doc") != -1) {
                        return '<img src="../../layuiadmin/images/word.png"/>';
                      } else if (item.indexOf(".jpg") != -1) {
                        return '<img src="../../layuiadmin/images/word.png"/>';
                      }
                    }
                    $(".otherFilesList").html(itemFileTitle);
                    var changeId = []; //所选中的文件id
                    $(".otherFilesList dd").on("click", function () {
                      var changeItem = $(this);
                      var changeItemId = changeItem.attr("data-id"); //当前文件id
                      if (changeItem.hasClass("changeFile")) {
                        let index = changeId.indexOf(changeItemId);
                        index > -1 && changeId.splice(index, 1);
                        console.log("removeClass", changeId);
                        changeItem.removeClass("changeFile");
                      } else {
                        console.log("addClass", changeId);
                        changeItem.addClass("changeFile");
                        changeId.push(changeItem.attr("data-id"));
                      }
                      console.log(changeId);
                    });
                    $(".fileChangeBtn").on("click", function () {
                      layer.closeAll();
                      filesAllListFun(changeId, proID);
                    });
                  }
                },
              });
            },
          });
        }
      });
    }
    // 控制页面文件夹是否可以移动
    layui.sessionData("isMove", {
      key: "key",
      value: false,
    });
    $(".removeBtn").on("click", function () {
      if (layui.sessionData("isMove").key) {
        layui.sessionData("isMove", {
          key: "key",
          value: false,
        });
        $(this).addClass("layui-btn-primary");
      } else {
        layui.sessionData("isMove", {
          key: "key",
          value: true,
        });
        $(this).removeClass("layui-btn-primary");
      }
    });

    function filesAllListFun(changeId, proID) {
      layer.open({
        title: "全部项目",
        type: 1,
        fix: false,
        resize: false,
        skin: "nextLevelFileSty",
        // 加上边框
        area: ["40%", "80%"],
        // 宽高
        content: $("#changeAllFile").html(),
        success: function (layero, index) {
          $.ajax({
            async: false,
            url: base_url + "/projectManage/selMovePro",
            headers: {
              Accept: "application/json; charset=utf-8",
              Authorization: layui.sessionData("userToken").key,
            },
            method: "post",
            contentType: "application/json",
            data: JSON.stringify({
              proID: proID,
            }),
            dataType: "json",
            success: function (result) {
              if (result.code == "200") {
                var filesAllData = result.data.proList,
                  itemFileTitle = "",
                  itemFile = "";
                filesAllData.forEach((item, index) => {
                  itemFile +=
                    '<li  data-id="' +
                    item.proID +
                    '" class="layui-nav-item layui-nav-itemed projectAllItem"><a href="javascript:;"><img src="../../layuiadmin/images/filesIcon.png"/>' +
                    item.proNmae +
                    "</a></li>";
                  $(".otherFilesList").html(itemFile);
                });
                $(".projectAllItem").on("click", function () {
                  var changeItem = $(this);
                  var changeItemId = changeItem.attr("data-id"); //当前文件id

                  if (changeItem.hasClass("changeFile")) {
                  } else {
                    projectAllChangeFun(changeId, changeItemId, $(this).text());
                    // layer.closeAll();
                  }
                });
              }
            },
          });
        },
      });
    }

    function projectAllChangeFun(changeId, changeItemId, projectName) {
      var otherLayer = layer.open({
        title: projectName,
        type: 1,
        fix: false,
        resize: false,
        skin: "nextLevelFileSty",
        // 加上边框
        area: ["40%", "80%"],
        // 宽高
        content: $("#changeFileNext").html(),
        success: function (layero, index) {
          $.ajax({
            async: false,
            url: base_url + "/projectManage/selMoveFile",
            headers: {
              Accept: "application/json; charset=utf-8",
              Authorization: layui.sessionData("userToken").key,
            },
            method: "post",
            contentType: "application/json",
            data: JSON.stringify({
              proID: changeItemId,
            }),
            dataType: "json",
            success: function (result) {
              if (result.code == "200") {
                var moveData = result.data,
                  itemFileTitle = "",
                  itemFile = "";
                var moveDataList = moveData["其他"];
                if (moveDataList && moveDataList.length > 0) {
                  for (let item in moveDataList) {
                    itemFile +=
                      ' <dd data-id="' +
                      moveDataList[item].id +
                      '"><a href="javascript:;">' +
                      fileSuffixFun(moveDataList[item].fileName) +
                      moveDataList[item].fileName +
                      "</a></dd>";
                  }
                  itemFileTitle =
                    '<li class="layui-nav-item layui-nav-itemed"><a href="javascript:;"><img src="../../layuiadmin/images/filesIcon.png"/>其他</a><dl class="layui-nav-child">' +
                    itemFile +
                    "</dl></li>";
                  $(".projectFilesList .fileChangeBtn").show();
                } else {
                  $(".projectFilesList .fileChangeBtn").hide();
                  itemFileTitle =
                    '<li class="layui-nav-item layui-nav-itemed"><a href="javascript:;"><img src="../../layuiadmin/images/filesIcon.png"/>其他</a><dl class="layui-nav-child"><dd style="font-size:14px;color:#999999;text-align:center;">空文件夹</dd></dl></li>';
                }
                function fileSuffixFun(item) {
                  if (item.indexOf(".doc") != -1) {
                    return '<img src="../../layuiadmin/images/word.png"/>';
                  } else if (item.indexOf(".jpg") != -1) {
                    return '<img src="../../layuiadmin/images/word.png"/>';
                  }
                }
                $(".otherFilesList").html(itemFileTitle);
                $(".fileOtherChangeBtn").on("click", function () {
                  $.ajax({
                    async: true,
                    url: base_url + "/projectManage/moveFolder",
                    headers: {
                      Accept: "application/json; charset=utf-8",
                      Authorization: layui.sessionData("userToken").key,
                    },
                    method: "post",
                    contentType: "application/json",
                    data: JSON.stringify({
                      proID: changeItemId,
                      moveFileID: changeId,
                    }),
                    dataType: "json",
                    success: function (result) {
                      if (result.code == "200") {
                        layer.msg(result.message, {
                          time: 1000,
                          icon: 6,
                          end: function () {
                            layer.closeAll();
                          },
                        });
                      } else {
                        layer.msg(result.message, { icon: 2 });
                      }
                    },
                  });
                  layer.close(otherLayer);
                });
              }
            },
          });
        },
      });
    }

    // 一级目录列表
    function handleOneLevelList(proID) {
      return new Promise((resolve, reject) => {
        $.ajax({
          async: false,
          url: base_url + "/projectManage/selMoveFile",
          headers: {
            Accept: "application/json; charset=utf-8",
            Authorization: layui.sessionData("userToken").key,
          },
          method: "post",
          contentType: "application/json",
          data: JSON.stringify({
            proID: proID,
          }),
          dataType: "json",
          success: function (result) {
            if (result.code == "200") {
              console.log(result);
              oneLevelCount = result.data.projectList.total;
              oneLevelData = result.data.projectList.list.length
                ? result.data.projectList.list
                : [];
              // console.log("一级列表", resCount, resData);
              let param = { oneLevelCount, oneLevelData };
              resolve(param);
            } else {
              reject(result.message);
            }
          },
        });
      });
    }
    // 二级目录：展示其他文件夹
    /**
     *
     * @param {String} proID 项目ID
     * @param {String} proType 项目类型
     * @returns
     */
    function handleTwoLevelList(proID, proType) {
      return new Promise((resolve, reject) => {
        $.ajax({
          async: false,
          url: base_url + "/projectManage/queryProCateByProID",
          headers: {
            Accept: "application/json; charset=utf-8",
            Authorization: layui.sessionData("userToken").key,
          },
          method: "post",
          contentType: "application/json",
          data: JSON.stringify({ proID, proType }),
          dataType: "json",
          success: function (result) {
            if (result.code == "200") {
              console.log(result);
              twoLevelData = result.data.project.length
                ? result.data.project
                : [];
              resolve(twoLevelData);
            } else {
              reject(result.message);
            }
          },
        });
      });
    }
    //  表格数据接口
    function renderProduct1(
      page = 1,
      limit = 40,
      startTime = "",
      endTime = "",
      projectName = ""
    ) {
      loadIndex = layer.load(2);
      $.ajax({
        async: false,
        url: base_url + "/projectManage/queryProListByProperty",
        headers: {
          Accept: "application/json; charset=utf-8",
          Authorization: layui.sessionData("userToken").key,
        },
        method: "post",
        contentType: "application/json",
        data: JSON.stringify({
          limit,
          page,
          sort: "1",
          startTime,
          endTime,
          projectName,
        }),
        dataType: "json",
        success: function (result) {
          resCount = result.data.projectList.total;
          resData = result.data.projectList.list;
          renderProductHtml(resData);
          renderPage1(page, limit, startTime, endTime, projectName); // 分页
        },
      });
    }
    // 独立分页功能
    function renderPage1(page, limit, startTime, endTime, projectName) {
      laypage.render({
        elem: "layuipage",
        count: resCount,
        limit: limit,
        limits: [limit],
        curr: page,
        theme: "#334d80",
        layout: ["prev", "page", "next", "skip", "count", "limit"],
        prev: '<i class="layui-icon"></i>',
        next: '<i class="layui-icon"></i>',
        jump: function (obj, first) {
          page = obj.curr;
          if (!first) {
            renderProduct1(page, limit, startTime, endTime, projectName);
          }
        },
      });
    }

    exports("projectManagement", {});
  }
);
